package rpg.writer.scripts.impl;

import rpg.client.model.Position;
import rpg.engine.model.GameEffect;
import rpg.engine.model.GameEffect.Type;
import rpg.writer.scripts.ScriptWarning;

public final class GameEffectTool {
	private static final String MY_PREFIX = ScriptAnalyzer.PREFIX_EFFECT;

	public static GameEffect parse(final Integer lineNumber, final String line,
			final AnalyzerContext context) {
		if (!line.startsWith(MY_PREFIX)) {
			context.addWarning(ScriptWarning.inputBad(lineNumber, line,
					"Business logic failure, line does not begin with "
							+ MY_PREFIX));
			return null;
		}

		final GameEffect.Builder geb = new GameEffect.Builder();

		geb.storyID(context.getStoryID());

		final int space0 = line.indexOf(" ");
		final int space1 = line.indexOf(" ", space0 + 1);
		final int space2 = line.indexOf(" ", space1 + 1);

		String typeCode = line.substring(1 + space0, space1);
		if (typeCode.equals("DEACTIVATE")) {
			typeCode = Type.OBJECT_DEACTIVATE.name();
		} else if (typeCode.equals("ACTIVATE")) {
			typeCode = Type.OBJECT_ACTIVATE.name();
		}

		final GameEffect.Type get;
		try {
			get = GameEffect.Type.valueOf(typeCode);
		} catch (final RuntimeException re) {
			context.addWarning(ScriptWarning.notRecognized(lineNumber, line,
					"TBD"));
			return null;
		}

		geb.type(get);

		switch (get) {
		case PLAYER_MAP_CHANGE:
			final Position.Builder pb = new Position.Builder();
			pb.map(line.substring(space1 + 1, space2));

			final int b1 = line.indexOf("(");
			final int b2 = line.indexOf(")", b1 + 1);
			final String positionInfo = line.substring(b1 + 1, b2);
			ScriptAnalyzer.xyz(pb, positionInfo);
			final Position p = pb.build();
			geb.to(p);
			break;
		case INVENTORY_ADD:
		case INVENTORY_REMOVE:
			final String fromID = context.getStoryID() + ":"
					+ line.substring(space1 + 1, line.length() - 1);
			geb.fromID(fromID);
			break;
		case TOKEN_ADD:
		case CHANGE_ACT:
			// [GameEffect TOKEN_ADD I kissed a boy]
			final String tokenID = line
					.substring(space1 + 1, line.length() - 1);
			geb.fromID(tokenID);
			break;
		case NPC_MOVE:
			// [GameEffect NPC_MOVE Wizard VOID]
			final String npcID = line.substring(space1 + 1, space2);
			geb.fromID(npcID);

			if (line.endsWith("VOID]")) {
				geb.to(Position.VOID);
			} else {
				context.addWarning(ScriptWarning.notImplemented(lineNumber,
						line, "TBD"));
			}
			break;
		case GENDER_CHANGE:
			geb.fromID(line.substring(space1 + 1, space2));
			geb.toID(line.substring(space2 + 1, line.length() - 1));
			break;
		default:
			final String objectID = line.substring(space1 + 1,
					line.length() - 1);
			geb.fromID(context.getStoryID() + ":" + objectID);
			break;
		}

		return geb.build();
	}

	public static GameEffect startChat(final Integer lineNumber,
			final String line, final AnalyzerContext context) {
		return null;
	}

	private GameEffectTool() {
		super();
	}
}
